
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class StatisticalData 
{
    StatisticalData(){}
    public double getMathematicalExpectation(List elements)
    {
        int average_value = 0;
        double mathematical_expectation = 0.0;
        Iterator elements_iterator = elements.iterator();
        while(elements_iterator.hasNext())
        {
            average_value += Integer.parseInt(elements_iterator.next().toString());
        }
        if(elements.size() > 0)
            mathematical_expectation = (double)average_value/(double)elements.size();
        return mathematical_expectation;
    }
    public double getDispersion(double mathematical_expectation, List elements)
    {
        Iterator elements_iterator = elements.iterator();
        double quadratic_difference = 0.0;
        double dispersion = 0.0;
        while(elements_iterator.hasNext())
        {
            double element = Double.parseDouble(elements_iterator.next().toString());
            quadratic_difference += Math.pow((Math.abs(mathematical_expectation) - Math.abs(element)), 2.0);
        }
        dispersion = quadratic_difference/(elements.size() - 1);
        return dispersion;
    }
    public double getSigma(double dispersion)
    {
        double sigma = Math.sqrt(dispersion);
        return sigma;
    }
    public double getAverageSigma(double sigma, int elements_count)
    {
        double average_sigma = sigma/Math.sqrt(elements_count);
        return average_sigma;
    }
    public double getStyudentsCoefficient(Double reliability, int degrees_freedom_count)
    {
        StyudentsTable styudents_table = new StyudentsTable();
        double styudents_coefficient = styudents_table.getStyudentsCoefficient(degrees_freedom_count, reliability);
        return styudents_coefficient;
    }
    public double getConfidenceInterval(double styudents_coefficient, double average_sigma)
    {
        double confidence_interval = styudents_coefficient*average_sigma;
        return confidence_interval;
    }
    public double getLowerLimit(double mathematical_expectation, double confidence_interval)
    {
        double lower_limit = mathematical_expectation - confidence_interval;
        return lower_limit;
    }
    public double getUpperLimit(double mathematical_expectation, double confidence_interval)
    {
        double upper_limit = mathematical_expectation + confidence_interval;
        return upper_limit;
    }
    public double getRelativeMistake(double mathematical_expectation, double confidence_interval)
    {
        double relative_mistake = (double)(confidence_interval/Math.abs(mathematical_expectation))*100;
        return relative_mistake;
    }
    public ArrayList<Object> getAllStatisticData(int number, List elements, Double reliability)
    {
        ArrayList<Object> statistic_data = new ArrayList<Object>();
        int elements_count = elements.size();
        double mathematical_expectation = getMathematicalExpectation(elements);
        double dispersion = getDispersion(mathematical_expectation, elements);
        double sigma = getSigma(dispersion);
        double average_sigma = getAverageSigma(sigma, elements_count);
        int degree_freedom_count = elements_count - 1;
        double styudents_coefficient = getStyudentsCoefficient(reliability, degree_freedom_count);
        double confidence_interval = getConfidenceInterval(styudents_coefficient, average_sigma);
        double upper_limit = getUpperLimit(mathematical_expectation, confidence_interval);
        double lower_limit = getLowerLimit(mathematical_expectation, confidence_interval);
        double relative_mistake = getRelativeMistake(mathematical_expectation, confidence_interval);
        
        statistic_data.add(number); 
        statistic_data.add(elements_count);
        statistic_data.add(mathematical_expectation);
        statistic_data.add(average_sigma);
        statistic_data.add(styudents_coefficient);
        statistic_data.add(confidence_interval);
        statistic_data.add(relative_mistake);

        return statistic_data;
    }
}
